//
//  UITextField+Ext.swift
//  TTCOM-iOS
//
//  Created by Toj on 10/26/22.
//


import UIKit

public extension UITextField {
    
    /**
     设置占位文字的颜色
     - parameter color: 颜色
     */
    func placeholderTextColor(color: UIColor) {
        setValue(color, forKeyPath: "_placeholderLabel.textColor")
    }
    
    /**
     左面间隔
     - parameter width: 左边的间隙
     */
    func leftMargin(width: CGFloat) {
        leftViewMode = .always
        let newLeftView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: frame.size.height))
        newLeftView.backgroundColor = UIColor.clear
        leftView = newLeftView
    }
}

public extension UITextField {
    
    func delete(shouldChangeCharactersIn range: NSRange, replacementString string: String, maxLength: Int, formatType: String.SPFormatterType = .threeFourFour, separator: String) -> Bool {
        
        let length = range.length
        if length == 0 { return true }
        
        let location = range.location
        
        var textStr = text ?? ""
        var textCount = textStr.count
        // 删除光标前最后一个字符, 如果是选中某些字符则删除选中部分
        deleteBackward()
        
        // 删除1个字符
        if length == 1 {
            // 从最后删除
            if location == textCount - 1 {
                textStr = text ?? ""
                textCount = textStr.count
                // 删没了
                if textCount == 0 { return false }
                
                // 最后一个字符是占位字符
                if textStr.lastCharString == separator {
                    deleteBackward()
                }
                return false
            }
        }
        
        // 从中间删除
        textStr = text ?? ""
        text = textStr.cm_numberFormat(format: formatType)
        // 重置光标位置 = 当前光标位置
        guard let newPos = position(from: beginningOfDocument, offset: location) else { return false }
        selectedTextRange = textRange(from: newPos, to: newPos)
        return false
    }
    
    // 粘贴有问题, 可以把粘贴禁用
    func insert(shouldChangeCharactersIn range: NSRange, replacementString string: String, maxLength: Int, formatType: String.SPFormatterType = .threeFourFour, separator: String) -> Bool {
        
        // 当前位置
        let location = range.location
        // 偏移量 = 当前光标位置 + 输入字符数量
        var offset = location + string.count
        
        // 输入前: 内容
        var textStr = text ?? ""
        // 输入前: 内容数量
        let textCount = textStr.count
        
        // 输入前: 光标前面内容
        let locationBeforString = textStr.cm_subsring(to: location)
        // 输入后: 光标前字符数量 = 输入前 光标前字符串数量 + 输入的数量
        let locationBeforCount = locationBeforString.count + string.count
        
        let totalString = textStr.to_cocoaString.replacingCharacters(in: range, with: string)
        var originalString = totalString.replacingOccurrences(of: separator, with: "")
        if originalString.count > maxLength {
            let locationAfterString = totalString.cm_subsring(to: offset).cm_numberFormat(format: formatType)
             
            // 超出最大数量 中间输入 有问题
            originalString = originalString.cm_subsring(to: maxLength)
            text = originalString.cm_numberFormat(format: formatType)
            offset = locationAfterString.count
        }
        else {
            insertText(string)
            textStr = text ?? ""
            
            let beforCount = textStr.count
            let locationAfterString = textStr.cm_subsring(to: offset).cm_numberFormat(format: formatType)
            
            text = textStr.cm_numberFormat(format: formatType)
            
            textStr = text ?? ""
            let afterCount = textStr.count
            
            // 最后插入
            if location == textCount  {
                if afterCount != beforCount {
                    offset += (afterCount - beforCount)
                }
            }
            else {
                let locationAfterCount = locationAfterString.count
                offset += (locationAfterCount - locationBeforCount)
            }
        }
        
        guard let newPos = position(from: beginningOfDocument, offset: offset) else { return false }
        selectedTextRange = textRange(from: newPos, to: newPos)

        return false
    }
}
